package org.ascollada.core
{
	public class DaeElement
	{
		/** */
		public var document	:DaeDocument;
		
		/** */
		public var id		:String;
		
		/** */
		public var sid		:String;
		
		/** */
		public var name		:String;
		
		/**
		 * Constructor.
		 * 
		 * @param	node Optional XML node.
		 */ 
		public function DaeElement( document:DaeDocument, node:XML=null )
		{
			this.document = document;
			if ( node )
				read( node );
		}
		
		/**
		 * Reads the node.
		 * 
		 * @param	node
		 */ 
		public function read( node:XML ) : void
		{
			this.id = node.@id.toString();
			this.sid = node.@sid.toString();
			this.name = node.@name.toString();
		}
		
		/**
		 * 
		 */
		public function readBools( node:XML ) : Array
		{
			var values : Array = readStrings( node );
			
			for ( var j:int = 0; j < values.length; j++ )
				values[j] = (values[j] == "true" || values[j] == "1") ? true : false;
				
			return values;	
		}
		
		/**
		 * 
		 */
		public function readFloats( node:XML ) : Array
		{
			var values : Array = readStrings( node );
			
			for ( var j:int = 0; j < values.length; j++ )
				values[j] = parseFloat( values[j] );
				
			return values;	
		}
		
		/**
		 * 
		 */
		public function readInts( node:XML ) : Array
		{
			var values : Array = readStrings( node );
			
			for ( var j:int = 0; j < values.length; j++ )
				values[j] = parseInt( values[j], 10 );
				
			return values;	
		}
		
		/**
		 * 
		 */
		public function readStrings( node:XML ) : Array
		{
			var raw : String = trimString( node.text().toString() );
			
			return raw.split( /\s+/ );	
		} 
		
		/**
		 * Validate the element to be read.
		 * 
		 * @param	nodeName
		 * 
		 * @return
		 */ 
		public function validateElement( nodeName:String ) : Boolean
		{
			return true;	
		}
		
		/**
		 * 
		 */ 
		public function stripPound( str:String ) : String
		{
			return (str.indexOf("#") == 0 ? str.substr(1) : str);
		}
		
		/**
		 * 
		 */
		public function trimString( str:String ) : String
		{
			str = str.replace(/^\s+/, "");
			str = str.replace(/\s+$/, "");
			return str;
		} 
	}
}